<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/4
     * Time: 下午3:29
     */

    namespace app\payment\model;

    use app\common\utils\UtilEncryption;
    use app\face2market\model\Face2marketOrderInfoModel;
    use app\face2market\model\Face2marKetPlatformFeeModel;
    use think\Model;
    use think\Db;

    class PaymentWithdrawModel extends Model
    {

        protected $name = 'payment_withdraw';

        // 自动写入时间戳
        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i','update_time' => 'timestamp:Y-m-d H:i'];

        /**
         * @return $this
         */
        static public function getObjectData()
        {

            return Db::view('payment_withdraw',true)//字段
            ->view('member','phone','member.id = payment_withdraw.member_id','left');

        }

        /**
         * 提交提现
         *
         * @param $data
         *
         * @return bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function addwithdraw($data)
        {

            $payment_info = PaymentInfoModel::where(['member_id' => $data['member_id']])->find();
            $order        = [];
            switch($data['type'])
            {
                case 1:
                    $order['wxpay_name']    = $payment_info['wxpay_name'];
                    $order['wxpay_account'] = $payment_info['wxpay_account'] * 100;
                    break;
                case 2:
                    $order['aliplay_name']    = $payment_info['aliplay_name'];
                    $order['aliplay_account'] = $payment_info['aliplay_account'];
                    break;
                case 3:
                    $order['bank_name']        = $payment_info['bank_name'];
                    $order['bank_card_number'] = $payment_info['bank_card_number'];
                    $order['bank_ture_name']   = $payment_info['bank_ture_name'];
                    break;
            }


            $config                = module_config('payment');
            $data['money']         = $data['money'] * 100; // 金额转换
            $order['order_sn']     = UtilEncryption::MakeSn();
            $order['type']         = $data['type'];
            $order['member_id']    = $data['member_id'];
            $order['amount_money'] = $data['money'];
            $order['up_amount_money'] = $data['money'];
            $order['proc_fee']     = round($data['money'] * $config['withdraw_price'],2);
            $order['true_money']   = $data['money'] - $order['proc_fee'];
            $order['point']        = 0;
            $order['status']       = 0;
            $marketOrder           = [
                'seller' => [
                    'seller_id' => $data['member_id']
                ],
                'buyer'  => [
                    'buyer_id' => $data['member_id']
                ],
                'pro_id' => 1,
                'number' => 1,

            ];
            $old                   = Face2marketOrderInfoModel::user_info($marketOrder);
            try
            {
                Db::startTrans();
                WalletModel::where(['member_id' => $data['member_id']])->setDec('money',$order['amount_money']);

                $PaymentWithdraw             = PaymentWithdrawModel::create($order);
                $new                         = Face2marketOrderInfoModel::user_info($marketOrder);
                $marketOrder['seller']['id'] = $PaymentWithdraw['id'];
                $marketOrder['buyer']['id']  = $PaymentWithdraw['id'];
                $marketOrder['order_info']   = $PaymentWithdraw;
                Face2marketOrderInfoModel::create_info($marketOrder,$old,$new,4);
                //平台手续费
                Face2marKetPlatformFeeModel::where(['id' => 3])->setInc('proc_money',$order['proc_fee']);

                Db::commit();
                return true;
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return false;
            }
        }

        /**
         * 撤销提现
         *
         * @param     $order_sn 订单编号
         * @param int $status   订单状态
         *
         * @return bool
         */
        static public function backout($order_sn,$status = 3)
        {

            try
            {
                Db::startTrans();

                $PaymentWithdrawModel         = PaymentWithdrawModel::where(['order_sn' => $order_sn])->find();
                $marketOrder                  = [
                    'seller'     => [
                        'seller_id' => $PaymentWithdrawModel['member_id'],
                        'id'        => $PaymentWithdrawModel['id'],
                    ],
                    'buyer'      => [
                        'buyer_id' => $PaymentWithdrawModel['member_id'],
                        'id'       => $PaymentWithdrawModel['id'],
                    ],
                    'pro_id'     => 1,
                    'number'     => 1,
                    'order_info' => $PaymentWithdrawModel,

                ];
                $old                          = Face2marketOrderInfoModel::user_info($marketOrder);
                $PaymentWithdrawModel->status = $status;
                $PaymentWithdrawModel->save();
                WalletModel::where(['member_id' => $PaymentWithdrawModel->member_id])->setInc('money',$PaymentWithdrawModel->amount_money);
                $new = Face2marketOrderInfoModel::user_info($marketOrder);
                Face2marketOrderInfoModel::create_info($marketOrder,$old,$new,5);
                Db::commit();
                return true;
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return false;
            }

        }

    }